﻿using ExamDemo.Data;
using ExamDemo.Models;
using ExamDemo.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ExamDemo.Controllers
{
    public class HomeController : Controller
    {
        private MyDb db = new MyDb();
        // GET: Home
        [HttpGet]
        public ActionResult Index()
        {
            if (Session["UserName"] == null)
            {
                return RedirectToRoute(new { controller = "user", action = "login" });

            }

            var messages = db.Messages.OrderByDescending(m => m.Id).ToList();

            var ef = from c in db.Comments
                     join m in db.Messages on c.MessageId equals m.Id
                     join u in db.Users on m.FromUserId equals u.Id
                     join u2 in db.Users on m.FromUserId equals u2.Id
                     select new { ssId = m.Id, c.Comment, ToUserName = u.UserName, FromUserName = u2.UserName };


            var messModel = new List<MessageModel>();

            foreach (var ss in messages)
            {
                var commentModel = new List<CommentModel>();

                foreach (var com in ef)
                {
                    if (com.ssId == ss.Id)
                    {
                        commentModel.Add(new CommentModel
                        {
                            Comment = com.Comment,
                            FromUserName = com.FromUserName,
                            ToUserName = com.ToUserName

                        });
                    }
                }

                messModel.Add(new MessageModel
                {
                    FromUserId = (int)Session["Id"],
                    Id = ss.Id,
                    Message = ss.Message,
                    CommentModels = commentModel
                });
            }


            return View(new IndexModel
            {
                UserName = Session["UserName"].ToString(),
                MessageModels=messModel
            }); 
        }


        [HttpPost]
        public ActionResult Say(string sayMsg)
        {
            dynamic result;
            if (sayMsg.Trim().Length > 0)
            {
              var message=  db.Messages.Add(new Messages
                {
                    Message=sayMsg,
                    FromUserId=(int)Session["Id"]
                    
                });

                db.SaveChanges();

                result = new
                {
                    code = 200,
                    content = message.Message,
                    id = message.Id,
                    msg="发表成功"
                };
            }
            else
            {
                result = new
                {
                    code = 2343,
                    msg = "发表失败"
                };
            }

            return Json(result);
        }

        public ActionResult Comment(string commentMsg, int msgId)
        {
            dynamic result;
            if (commentMsg.Trim().Length > 0)
            {
              var com=  db.Comments.Add(new Comments
                {
                    Comment = commentMsg,
                    FromUserId = (int)Session["Id"],
                    MessageId = msgId
                    
                });

              var userId=  db.Messages.Find(com.MessageId).FromUserId;
                var userName = db.Users.Find(userId).UserName;

                db.SaveChanges();


                result = new
                {
                    code = 200,
                    content = com.Comment,
                    fromUserName = Session["UserName"].ToString(),
                    toUserName = userName,
                    msgId = msgId
                };
            }
            else
            {
                result = new
                {
                    code = 2343,
                    msg = "评论失败"
                };
            }

            return Json(result);

        }

        public void Logout()
        {
            Session.Clear();
        }
    }
}